package com.wjf.accesscontrol.ui

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import com.wjf.accesscontrol.R
import com.wjf.accesscontrol.base.MVVMBaseActivity
import com.wjf.accesscontrol.databinding.ActivityTitleBarBinding
import com.wjf.accesscontrol.receiver.time.TimeCallback
import com.wjf.accesscontrol.receiver.time.TimeReceiver
import com.wjf.accesscontrol.ui.main.MainFragment
import com.wjf.accesscontrol.ui.read.IdCardFragment
import com.wjf.accesscontrol.ui.setting.LoginFragment
import com.wjf.accesscontrol.ui.setting.SettingFragment
import com.wjf.accesscontrol.ui.read.SocialFragment
import com.wjf.accesscontrol.ui.setting.ActivateFragment
import com.wjf.accesscontrol.util.COMMON_FLAG_DATA
import com.wjf.accesscontrol.util.COMMON_FLAG_MSG
import com.wjf.accesscontrol.util.Constant
import com.wjf.accesscontrol.util.JUMP_TO
import com.wjf.accesscontrol.util.JUMP_TO_ActivateFragment
import com.wjf.accesscontrol.util.JUMP_TO_IdCardFragment
import com.wjf.accesscontrol.util.JUMP_TO_LoginFragment
import com.wjf.accesscontrol.util.JUMP_TO_SettingFragment
import com.wjf.accesscontrol.util.JUMP_TO_SocialFragment
import com.wjf.accesscontrol.util.TimeUtils
import com.wjf.accesscontrol.util.continuousClick

class TitleBarActivity : MVVMBaseActivity<TitleBarViewModel, ActivityTitleBarBinding>(), TimeCallback.TimeInterface {


    companion object{
        fun newInstance(context: Context, jumpTo: String, msg: String = "", data: String = ""){
            val intent = Intent(context, TitleBarActivity::class.java)
            intent.putExtra(JUMP_TO,jumpTo)
            intent.putExtra(COMMON_FLAG_MSG,msg)
            intent.putExtra(COMMON_FLAG_DATA,data)
            context.startActivity(intent)
        }
    }

    lateinit var fragment: Fragment

    override fun initViewBinding(inflater: LayoutInflater) = ActivityTitleBarBinding.inflate(inflater)


    override fun initViewModel() = ViewModelProviders.of(this).get(
        TitleBarViewModel::class.java)

    override fun initView(){

        fragment = when(intent.getStringExtra(JUMP_TO)) {

            JUMP_TO_IdCardFragment -> { IdCardFragment() }

            JUMP_TO_SocialFragment -> { SocialFragment() }

            JUMP_TO_LoginFragment -> { LoginFragment() }

            JUMP_TO_SettingFragment -> { SettingFragment.newInstance() }

            JUMP_TO_ActivateFragment -> {
                ActivateFragment.newInstance(
                    msg = "${intent.getStringExtra(COMMON_FLAG_MSG)}",
                )
            }

            else -> { MainFragment() }
        }

        supportFragmentManager.beginTransaction().replace(R.id.info_content, fragment).commit()

    }

    override fun onResume() {
        super.onResume()
        binding.layoutHeader.tvTime.text = TimeUtils.instance.getTime(pattern = "HH:mm  MM月dd日 EE")
        binding.layoutHeader.tvTopStationName.text = Constant.stationName
    }

    fun setStationName(stationName: String){
        binding.layoutHeader.tvTopStationName.text = stationName
    }

    override fun initData() {
        lifecycle.addObserver(TimeReceiver(this))
    }


    override fun initClick(){

        binding.layoutHeader.ivHome.continuousClick {
            if (fragment is MainFragment){
                newInstance(this,JUMP_TO_LoginFragment)
            }
        }
    }

    override fun updateTime(time: String) {
        binding.layoutHeader.tvTime.text = time
    }
}